﻿using System;
using log4net;
using Moreeasi.TaskQueue;
using Prism.Events;
using Microsoft.Extensions.Caching.Memory;
using Modules.MSentEvent;
using Sorting.App.Models;
using Sorting.App.Models.Enum;
using Sorting.App.Services;
using Modules.Units;
using Modules.ActionRun;
using Prism.Ioc;

namespace Lm.Core.TaskQueue;


/// <summary>
/// 任务处理主队列
/// </summary>
public class RunMainTask : UnitTask
{



    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="ea"></param>
    public RunMainTask(IEventAggregator ea, IContainerExtension container)
    {
        _ea = ea;
        _container = container;
    }
    IContainerExtension _container;
    public TaskLogService _logService = new TaskLogService();
    public ShelfService shelfService = new ShelfService();

    public ILog log = LogManager.GetLogger(typeof(RunMainTask));


    #region 属性

    IEventAggregator _ea;

    #endregion


    #region  公共方法



    #endregion

    #region 重载方法


    /// <summary>
    /// 任务处理
    /// </summary>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public override void ExecuteNew()
    {
        //var data = await shelfService.GetShelfByWardNo(this.Data);

        var data = (ShelfEntity)this.Data;




        //通知界面正在处理的任务
        _ea.GetEvent<ViewModelSentEvent>().Publish(new MessageModel
        {
            Code = EnumMessageType.ME107,
            Data = data,
            Message = "重新绘制界面"
        });


        //任务进入处理 
        this.State = EnumTaskState.Running;
        //下发储位信息
        ActionRunHelper.RunToLoc((short)Convert.ToInt32(data.Code));

        log.Debug($"模组{data.Code}储位写入时间:{DateTime.Now.ToString()}[{this.Name}-{this.TaskId}]");


        this.State = EnumTaskState.Sucess;

        this.ErrorMsg = $"模组中储位{data.Code}下发PLC成功！";
        return;


    }

    #endregion
}